import { reqGetActivityList, reqGetCategoryList, reqUpdateActivity, reqUpdateCategory } from '@/api/activity'

export default {
  namespaced: true,
  state: {
    activityList: [],
    pagination: {
      currentPage: 1,
      pageSize: 5,
      total: 0
    },
    activity: {},
    categoryList: [],
    categoryPagination: {
      currentPage: 1,
      pageSize: 5,
      total: 0
    },
    category: {}
  },
  mutations: {
    // 保存activityList
    GETACTIVITYLIST(state, activityList) {
      state.activityList = [...activityList]
    },
    // 保存activityCount
    SAVEACTIVITYCOUNT(state, pagination) {
      state.pagination = { ...pagination }
    },
    // 保存categoryList
    GETCATEGORYLIST(state, categoryList) {
      state.categoryList = [...categoryList]
    },
    // 保存categoryPagination
    SAVECATEGORYCOUNT(state, pagination) {
      state.categoryPagination = { ...pagination }
    },
    // 保存activity
    SAVEACTIVITY(state, activity) {
      state.activity = { ...activity }
    },
    // 保存category
    SAVECATEGORY(state, category) {
      state.category = { ...category }
    }
  },
  actions: {
    // 获取活动列表
    async getActivityList({ commit }, { pagination }) {
      const { currentPage, pageSize } = pagination.value || {}
      const result = await reqGetActivityList({ currentPage, pageSize })
      if (result.code === 200) {
        commit('GETACTIVITYLIST', result.data.data)
        commit('SAVEACTIVITYCOUNT', {
          currentPage,
          pageSize,
          total: result.data.length
        })
      }
    },
    // 获取活动类型列表
    async getCategoryList({ commit }, { pagination }) {
      const { currentPage, pageSize } = pagination.value || {}
      const result = await reqGetCategoryList({ currentPage, pageSize })
      if (result.code === 200) {
        commit('GETCATEGORYLIST', result.data.data)
        commit('SAVECATEGORYCOUNT', {
          currentPage,
          pageSize,
          total: result.data.length
        })
      }
    },
    // 修改/删除活动
    async updateActivity({ commit }, data) {
      const result = await reqUpdateActivity(data)
      if (result.code === 200 && result.data === 1) {
        return 'OK'
      } else {
        return Promise.reject(new Error('出错了！'))
      }
    },
    // 修改/删除活动类型
    async updateCategory({ commit }, data) {
      const result = await reqUpdateCategory(data)
      if (result.code === 200 && result.data === 1) {
        return result
      } else {
        return Promise.reject(new Error('出错了！'))
      }
    }
  }
}
